(function() {
	dorado.widget.YMPicker = $extend(dorado.widget.Control,{
		 $className: "dorado.widget.YMPicker",
		_inherentClassName: "i-year-month-picker",
		_opened: false,
		focusable: true,
		ATTRIBUTES:{
			className: {
				defaultValue: "d-year-month-picker"
			},
			year: {
				defaultValue: (new Date).getFullYear()
			},

			/**
			 * 选择的月份
			 * @attribute
			 * @default 0
			 * @type int
			 */
			month: {
				defaultValue: 0
			},
			yearstart:{
				
			},
			yearend:{
				
			}
		},
		EVENTS: /** @scope dorado.widget.YearMonthPicker.prototype */ {
			/**
			 * 当点击确定按钮之后触发的事件
			 * @param {Object} self 事件的发起者，即组件本身。
			 * @param {Object} arg 事件参数。
			 * @return {boolean} 是否要继续后续事件的触发操作，不提供返回值时系统将按照返回值为true进行处理。
			 * @event
			 */
			onPick: {},
			/**
			 * 当点击取消按钮之后触发的事件
			 * @param {Object} self 事件的发起者，即组件本身。
			 * @param {Object} arg 事件参数。
			 * @return {boolean} 是否要继续后续事件的触发操作，不提供返回值时系统将按照返回值为true进行处理。
			 * @event
			 */
			onCancel: {}
		},
		/**
		 * 刷新月份。
		 * @param {int} month 设置的月份。
		 * @private
		 */
		refreshMonth: function(month) {
			var picker = this, oldMonth = picker._month, doms = picker._doms, monthTable = doms.monthTable;
			
			if (isNaN(month)) {
				month = oldMonth ? oldMonth : 0;
			}
			
			var lastSelectedCell = picker._lastSelectedMonthCell, cell = monthTable.rows[Math.floor(month / 2)].cells[month % 2];
			
			if (lastSelectedCell) {
				$fly(lastSelectedCell).removeClass("selected");
			}
			
			if (cell) {
				$fly(cell).addClass("selected");
			}
			
			picker._lastSelectedMonthCell = cell;
		},

		/**
		 * 更新日期。
		 * @param date
		 * @param month
		 */
		updateDate: function(date, month) {
			var picker = this, year = date;
			
			if (date instanceof Date) {
				picker.set("year", date.getFullYear());
				picker.set("month", date.getMonth());
			} else {
				if (!isNaN(year) && !isNaN(month)) {
					picker.set("year", year);
					picker.set("month", month);
				}
			}
		},
		refreshYear: function(year) {
			var picker = this,yearstart=picker._yearstart,yearend=picker._yearend, oldYear = picker._year, startYear, doms = picker._doms, yearTable = doms.yearTable, remainder;
			
			if(yearstart && yearend){
			
				if(yearend - yearstart <=10 && yearend - yearstart >0){
					year = yearstart,startYear=year;
					for (var i = 0; i < 5; i++) {
						for (var j = 0; j < 2; j++) {
							var cell = yearTable.rows[i].cells[j];
							var realYear =yearstart + i * 2 + j;
							if(realYear <= yearend){
								cell.innerText = realYear;
							}else{
								cell.innerText ="";
							}
						}
					}
				}else if(yearend -yearstart >10){
					year = year ? year : oldYear;
					remainder = year % 10;
					startYear = year - (remainder == 0 ? 10 : remainder) + 1;
					
					for (var i = 0; i < 5; i++) {
						for (var j = 0; j < 2; j++) {
							var cell = yearTable.rows[i].cells[j];
							var realYear =startYear + i * 2 + j;
							if(realYear <= yearend && realYear>=yearstart){
								cell.innerText = realYear;
							}else{
								cell.innerText ="";
							}
						}
					}
				}
			}else{
				year = year ? year : oldYear;
				remainder = year % 10;
				startYear = year - (remainder == 0 ? 10 : remainder) + 1;
				
				for (var i = 0; i < 5; i++) {
					for (var j = 0; j < 2; j++) {
						var cell = yearTable.rows[i].cells[j];
						cell.innerText = startYear + i * 2 + j;
					}
				}
			}
			
			var lastSelectedCell = picker._lastSelectedYearCell, index = year - startYear, newSelectedCell = yearTable.rows[Math.floor(index / 2)].cells[index % 2];
			
			if (lastSelectedCell) {
				$fly(lastSelectedCell).removeClass("selected");
			}
			
			if (newSelectedCell) {
				$fly(newSelectedCell).addClass("selected");
			}
			
			picker._lastSelectedYearCell = newSelectedCell;
		},
		createDom: function() {
			var monthLabel = $resource("dorado.baseWidget.AllMonths"), month_label = monthLabel.split(",");
			var picker = this, doms = {}, dom = $DomUtils.xCreate({
				tagName: "div",
				content: [{
					tagName: "table",
					className: "year-table",
					contextKey: "yearTable",
					content: [
						{ tagName: "tr", className: "number-row", content: [{ tagName: "td" }, { tagName: "td" }] },
						{ tagName: "tr", className: "number-row", content: [{ tagName: "td" }, { tagName: "td" }] },
						{ tagName: "tr", className: "number-row", content: [{ tagName: "td" }, { tagName: "td" }] },
						{ tagName: "tr", className: "number-row", content: [{ tagName: "td" }, { tagName: "td" }] },
						{ tagName: "tr", className: "number-row", content: [{ tagName: "td" }, { tagName: "td" }] },
						{ tagName: "tr", className: "btn-row", content: [
							{ tagName: "td", align: "center", contextKey: "preaYearCell" },
							{ tagName: "td", align: "center", contextKey: "nextYearCell" }
						]}
					]
				}, {
					tagName: "table",
					className: "month-table",
					contextKey: "monthTable",
					content: [{
						tagName: "tr",
						className: "number-row",
						content: [
							{ tagName: "td", content: month_label[0] },
							{ tagName: "td", content: month_label[1] }
						]
					}, {
						tagName: "tr",
						className: "number-row",
						content: [
							{ tagName: "td", content: month_label[2] },
							{ tagName: "td", content: month_label[3] }
						]
					}, {
						tagName: "tr",
						className: "number-row",
						content: [
							{ tagName: "td", content: month_label[4] },
							{ tagName: "td", content: month_label[5] }
						]
					}, {
						tagName: "tr",
						className: "number-row",
						content: [
							{ tagName: "td", content: month_label[6] },
							{ tagName: "td", content: month_label[7] }
						]
					}, {
						tagName: "tr",
						className: "number-row",
						content: [
							{ tagName: "td", content: month_label[8] },
							{ tagName: "td", content: month_label[9] }
						]
					}, {
						tagName: "tr",
						className: "number-row",
						content: [
							{ tagName: "td", content: month_label[10] },
							{ tagName: "td", content: month_label[11] }
						]
					}]
				}, {
					tagName: "div",
					className: "btns-pane",
					contextKey: "buttonPanel"
				}]
			}, null, doms);
			
			var monthLastOverCell, yearLastOverCell;
			
			picker._doms = doms;
			
			if (dorado.Browser.msie && dorado.Browser.version == 6) {
				$fly(doms.monthTable).mousemove(function(event) {
					var position = $DomUtils.getCellPosition(event);
					
					if (position && position.element && (position.element != monthLastOverCell || !monthLastOverCell)) {
						if (monthLastOverCell) {
							$fly(monthLastOverCell).removeClass("hover");
						}
						$fly(position.element).addClass("hover");
						monthLastOverCell = position.element;
					}
				}).mouseout(function() {
					if (monthLastOverCell) {
						$fly(monthLastOverCell).removeClass("hover");
					}
					monthLastOverCell = null;
				});
			}
			
			$fly(doms.monthTable).click(function(event) {
				var position = $DomUtils.getCellPosition(event);
				if (position && position.element) {
					picker.set("month", position.row * 2 + position.column);
					picker.fireEvent("onPick", picker);
				}
			});
			
			if (dorado.Browser.msie && dorado.Browser.version == 6) {
				$fly(doms.yearTable).mousemove(function(event) {
					var position = $DomUtils.getCellPosition(event);
					
					if (position && position.element && (position.element != yearLastOverCell || !yearLastOverCell)) {
						if (position.row == 5) {
							return;
						}
						if (yearLastOverCell) {
							$fly(yearLastOverCell).removeClass("hover");
						}
						$fly(position.element).addClass("hover");
						yearLastOverCell = position.element;
					}
				}).mouseout(function() {
					if (yearLastOverCell) {
						$fly(yearLastOverCell).removeClass("hover");
					}
					yearLastOverCell = null;
				});
			}
			
			$fly(doms.yearTable).click(function(event) {
				var position = $DomUtils.getCellPosition(event);
				if (position && position.element) {
					if (position.row == 5) {
						return;
					}
					if("" != position.element.innerHTML ){
						picker.set("year", parseInt(position.element.innerHTML, 10));
					}
				}
			});
			
			var preYearButton = new dorado.widget.SimpleIconButton({
				iconClass: "prev-year-button",
				listener: {
					onClick: function() {
						picker.set("year", picker._year - 10);
					}
				}
			});
			
			var nextYearButton = new dorado.widget.SimpleIconButton({
				iconClass: "next-year-button",
				listener: {
					onClick: function() {
						picker.set("year", picker._year + 10);
					}
				}
			});
			
			preYearButton.render(doms.preaYearCell);
			nextYearButton.render(doms.nextYearCell);
			
			picker.registerInnerControl(preYearButton);
			picker.registerInnerControl(nextYearButton);
			
			var okButton = new dorado.widget.Button({
				caption: $resource("dorado.baseWidget.YMPickerConfirm"),
				listener: {
					onClick: function() {
						picker.fireEvent("onPick", picker);
					}
				}
			});
			okButton.render(doms.buttonPanel);
			
			var cancelButton = new dorado.widget.Button({
				caption: $resource("dorado.baseWidget.YMPickerCancel"),
				listener: {
					onClick: function() {
						picker.fireEvent("onCancel", picker);
					}
				}
			});
			cancelButton.render(doms.buttonPanel);
			
			picker.registerInnerControl(okButton);
			picker.registerInnerControl(cancelButton);
			
			picker.refreshYear();
			picker.refreshMonth();
			
			return dom;
		},

		refreshDom: function() {
			var picker = this;
			picker.refreshYear(picker._year);
			picker.refreshMonth(picker._month);
			$invokeSuper.call(this, arguments);
		}
	});
	
	dorado.widget.YMDropDown =$extend(dorado.widget.DropDown,{
		$className: "dorado.widget.YMDropDown",
		ATTRIBUTES: {
			width: {
				defaultValue: 260
			},
			iconClass: {
				defaultValue: "d-trigger-icon-date"
			},
			returnType:{
				defaultValue:"entity"
			},
			yearstart:{
				
			},
			yearend:{
				
			},
			preEditor:{
				
			}
		},
		createDropDownBox: function(editor) {
			var yearstart = this._yearstart,yearend = this._yearend;
			view = this.get('view'),preEditor = this.get('preEditor'),returnType=this.get('returnType');
			var dropDown = this, box = $invokeSuper.call(this, arguments), picker = new dorado.widget.YMPicker({
				listener: {
					onPick: function(picker) {
						var val = view.get('#'+preEditor+'.value');
						var month = picker._month+1;
						var retval = {
								year: picker._year,
								month: month,
								label: picker._year+$resource("ndp_base_year")+ month+$resource("ndp_base_month")
						};//new Date(picker._year, picker._month);
						if(picker._month <10){
							retval.code =picker._year+"-0"+month;
						}else{
							retval.code =picker._year+"-"+month;
						}
						var start = new Date(picker._year, picker._month);
						start.set({day:1,hour:0,minute:0,second:0});
						var end = new Date(picker._year, picker._month);
						end.set({hour:23,minute:59,second:59});
						end.set({day:Date.getDaysInMonth(picker._year,picker._month)});
						if(!returnType || "entity" == returnType || "obj" == returnType){
							retval.start = start;
							retval.end = end;
							dropDown.close(retval);
						}else if("code" == returnType){
							dropDown.close(retval.code);
						}else if("start" == returnType){
							if(val){
								if(start<=val){
									throw new dorado.Exception($resource("ndp_base_error_end"));
								}else{
									dropDown.close(start);
								}
							}else{
								dropDown.close(start);
							}
						}else if("end" == returnType){
							if(val){
								if(end<=val){
									throw new dorado.Exception($resource("ndp_base_error_end"));
								}else{
									dropDown.close(end);
								}
							}else{
								dropDown.close(end);
							}
						}
					},
					onCancel: function() {
						dropDown.close();
					}
				},
				yearstart:yearstart,
				yearend:yearend
			});
			
			box.set({
				style: {
					overflow: "hidden",
					height: 190
				},
				control: picker
			});
			return box;
		}
	});
})();